package avicit.bdp.dgs.metadata.compare.pojo;

import avicit.bdp.common.service.dto.BdpColumnDTO;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据表列变更明细
 *
 * @author mayi
 */
public class MetadataTableColumnDiffItem extends DiffItem<BdpColumnDTO> {

  public static final String NO = "NO";
  public static final String ADD = "ADD";
  public static final String DROP = "DROP";
  public static final String MODIFY = "MODIFY";
  public static final String CHANGE = "CHANGE";

  public MetadataTableColumnDiffItem(BdpColumnDTO ot, BdpColumnDTO nt) {
    super(ot, nt);

    if (nt != null) {
      setKey(nt.getName());
    } else if (ot != null) {
      setKey(ot.getName());
    }

    if (ot != null && nt == null) {
      // 删除字段
      setType(DROP);
    } else if (ot == null && nt != null) {
      // 添加字段
      setType(ADD);
    } else if (ot.getName().equals(nt.getName())) {
      // 修改字段
      // 使用原始ID
      nt.setId(ot.getId());
      setType(MODIFY);
      if (StringUtils.equals(ot.getColumnType(), nt.getColumnType())
          // && StringUtils.equals(ot.getColumnDefault(), nt.getColumnDefault())
          && Objects.equals(ot.getColumnLength(), nt.getColumnLength())
          && Objects.equals(ot.getColumnPrecision(), nt.getColumnPrecision())
          && Objects.equals(ot.getPrimaryKey(), nt.getPrimaryKey())
          && Objects.equals(ot.getNullable(), nt.getNullable())
          && Objects.equals(ot.getUniqueKey(), nt.getUniqueKey())
          // && StringUtils.equals(ot.getExtra(), nt.getExtra())
          && StringUtils.equals(ot.getRemark(), nt.getRemark())) {
        setType(NO);
      }
    } else {
      // 修改字段
      setType(CHANGE);
    }
  }
}
